/*
 	解除绑定
 * */
define([
    '../../common/service',
    '../../../app/framework/bean/connectionobjects',
    '../../../app/framework/bean/entrustaccounts',
    '../../../app/framework/bean/menu',
    '../../../app/framework/bean/organization',
    '../../../app/framework/bean/user',
    '../../../app/framework/bean/version',
    'jquery-html5Validate'
], function(
    http,
    connectionobjectsModel,
    entrustaccountsModel,
    menuModel,
    organizationModel,
    userModel,
    versionModel
) {
    var Unbind = {
        init: function(query) {
            this.initData();
            this.bindSubmitEvent();
        },
        initData: function() {
            var _this = this;

            var loadingArea = $('#unbindFbUserForm');;
            var labelEmail = $('#labelEmail');
            Kit.loading.show(loadingArea);
            http.work('/user/currentFbUser', 'POST').then(function(res) {
                var data = res.data;
                if (res.flag == 'success') {
                    labelEmail.html(data.email);
                } else {
                    Kit.alert2(res.msg);
                }
                Kit.loading.hide(loadingArea);
            });
        }

        /*
         @绑定提交事件
         含表单验证
         */
        ,
        bindSubmitEvent: function() {
            var _this = this;
            var myForm = $('#unbindFbUserForm');
            var pwd = myForm.find('#password');
            var submitForm = myForm.find('.submitForm');
            pwd.val('');
            submitForm.unbind('click');
            submitForm.click(function() {

                if ('' == pwd.val()) {
                    pwd.testRemind(I18N.getText('user', 'enterPassword'));
                    pwd.focus();
                    return false;
                }
                if (pwd.val().length < 6) {
                    pwd.testRemind(I18N.getText('user', 'passwordmin'));
                    pwd.focus();
                    return false;
                }
                var param = {
                    password: pwd.val()
                };

                var loadingArea = $('#unbindFbUserForm');
                Kit.loading.show(loadingArea);
                http.work('/user/unbindFbUser', param, 'POST').then(function(res) {
                    var data = res.data;
                    if (res.flag == 'success') {
                        /**
                         * 清空一些本地缓存
                         */
                        connectionobjectsModel.reset();
                        entrustaccountsModel.reset();
                        menuModel.reset();
                        organizationModel.reset();
                        userModel.reset();
                        versionModel.reset();

                        //window.location.href = "account.html";
                        window.location.hash.indexOf('account') != -1 ? '' : window.location.hash = '#!account';

                    } else {
                        Kit.alert2(res.msg)
                    }
                    Kit.loading.hide(loadingArea);
                });
            });
        }
    };
    return Unbind;
});
